package org.zjt.design.pattern;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static java.lang.System.out;
/**
 * @author <a href="juntao.zhang@qq.com">juntao.zhang</a>
 * @Description:
 * @Package org.zjt.design.pattern
 * @date 2018/5/8 16:31
 * @see
 */
public class AdapterTest {
    public static void main(String[] args) {
        MediaAdapter mediaAdapter = new MediaAdapter();
        mediaAdapter.play("sss.mp3");
    }
}


interface MediaPlayer{
    void play(String fileName);
}


class MediaAdapter implements MediaPlayer {

    private static final ConcurrentHashMap<String,MediaPlayer> cache = new ConcurrentHashMap();

    @Override
    public void play(String fileName){
        String ext = fileName.substring(fileName.indexOf(".") + 1);
        if (!cache.contains(ext))
            cache.put(ext, Objects.equals(ext,"mp3") ? new Mp3Player() : new VlcPlayer());
        cache.get(ext).play(fileName);
    }

}

class VlcPlayer implements MediaPlayer{

    @Override
    public void play(String fileName){
        out.println("VlcPlayer: "+fileName);
    }
}

class Mp3Player implements MediaPlayer{

    @Override
    public void play(String fileName){
        out.println("Mp3Player: "+fileName);
    }
}